package com.lzy.mall.search.thread;


import java.util.concurrent.*;

/**
 * @author lzy
 * @date 2021/9/19 12:01
 */
public class ThreadTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> async = CompletableFuture.supplyAsync(() -> {
            int i = 10/2;
            return i;
        });
//        CompletableFuture<Integer> result1 = async.exceptionally(throwable -> {
//            System.out.println(throwable);
//            return 5555;
//        });
//        CompletableFuture<Integer> result2 = async.whenCompleteAsync(((integer, throwable) -> {
//            System.out.println(integer + "||||" + throwable);
//        }), service);
//
//        System.out.println("01:" + result1.get());
//        System.out.println("02:" + result2.get());

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            int i = 10 / 0;
//            return i;
//        }, service)
//                .handle((res,e) -> {
//                    if (res != null){
//                        return res * 2;
//                    }
//                    if (e != null){
//                        System.out.println(e);
//                        return 0;
//                    }
//                    return 1000;
//                });
//        System.out.println(future.get());

//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一");
//            return 10;
//        })
//                .thenApplyAsync(res -> {
//                    System.out.println("任务2:" + res);
//                    return "hello," + res;
//                });
//                .thenAcceptAsync(res -> {
//                    System.out.println("任务2:" + res);
//                });
//                .thenRunAsync(() -> {
//                    System.out.println("任务二");
//                });
//        CompletableFuture<Integer> future = async.thenApplyAsync(integer -> {
//            System.out.println("thenApplyAsync:"+ integer);
//            return integer * 2;
//        }, executor);
//        System.out.println(future.get());
//
//        CompletableFuture<Void> future1 = async.thenAcceptAsync(integer -> {
//            System.out.println("thenAcceptAsync:" + integer);
//        }, executor);
//
//        CompletableFuture<Void> future2 = async.thenRunAsync(() -> {
//            System.out.println("新的线程操作 thenRun");
//        }, executor);
        //thenApplyAsync:5
        //10
        //thenAcceptAsync:5
        //新的线程操作 thenRun



        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync01");
            return 111;
        });


        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync02");
            return "hello";
        });

//        CompletableFuture<String> thenCombineAsync = future01.thenCombineAsync(future02, (f1, f2) -> {
//            System.out.println("thenCombineAsync::" + f1 + "-->" + f2);
//            return f1 + ":::" + f2;
//        }, executor);
//        System.out.println(thenCombineAsync.get());

//        CompletableFuture<Void> thenAcceptBothAsync = future01.thenAcceptBothAsync(future02, (f1, f2) -> {
//            System.out.println("thenAcceptBothAsync::" + f1 + "-->" + f2);
//        }, executor);
//        CompletableFuture<Void> runAfterBothAsync = future01.runAfterBothAsync(future02,
//                () -> System.out.println("runAfterBothAsync"), executor);

//        future01.applyToEither();
//        future01.acceptEither();
//        future01.runAfterEither();
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future01, future02);
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future01, future02);
        System.out.println(anyOf.get());

    }

        /**
     * 1.继承Thread
     */
    public static class Thread01 extends Thread{
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
        }
    }
}
